package pers.wwz.study.wechatserver.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import pers.wwz.study.wechatserver.common.DataResponse;
import pers.wwz.study.wechatserver.dto.MsgDTO;
import pers.wwz.study.wechatserver.feign.WechatFeign;
import pers.wwz.study.wechatserver.feign.request.SendSubscribeMgRequest;
import pers.wwz.study.wechatserver.feign.respoonse.AccessTokenResponse;
import pers.wwz.study.wechatserver.feign.respoonse.Code2SessionResponse;
import pers.wwz.study.wechatserver.feign.respoonse.SendSubscribeMsgResponse;
import pers.wwz.study.wechatserver.utils.subscribemsg.SubscribeMsgUtils;

/**
 * 微信控制器
 * 生成不限制的二维码官方地址：https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/qrcode-link/qr-code/getUnlimitedQRCode.html
 */

@Slf4j
@RestController
@RequestMapping("/api/wechat")
public class WechatController {
    @Value("${wechat.appid}")
    private String appid;

    @Value("${wechat.secret}")
    private String secret;

    @Value("${wechat.grant_type.accesstokenType}")
    private String accesstokenType;

    @Value("${wechat.grant_type.code2SessionType}")
    private String code2SessionType;

    @Autowired
    WechatFeign wechatFeign;

    /**
     * 获取accessToken
     * 官方参考地址：https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/Get_access_token.html
     * postman请求:localhost:8080/api/wechat/cgi-bin/token?access_token
     * postman无需任何参数
     * @return 结果
     */
    @RequestMapping("/cgi-bin/token")
    DataResponse getAccessToken(){
        AccessTokenResponse accessToken = wechatFeign.getAccessToken(appid, secret, accesstokenType);
        return DataResponse.succeed(accessToken.getAccess_token());
    }

    /**
     *
     * 根据code获取用户信息
     * 官方文档(服务端)：https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/user-login/code2Session.html
     * 官方文档(小程序)https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/login.html
     * @return 结果
     */
    @RequestMapping("/code2Session")
    DataResponse code2Session(@RequestParam String jsCode ){
        Code2SessionResponse code2SessionResponse = wechatFeign.jscode2session(appid, secret, jsCode, code2SessionType);
        return DataResponse.succeed(code2SessionResponse);
    }

    /**
     * 发送订阅消息
     * 参考地址:https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/mp-message-management/subscribe-message/sendMessage.html
     * @return 结果
     */
    @RequestMapping("/sendSubscribeMsg")
    DataResponse sendSubscribeMsg(@RequestBody MsgDTO msgDTO){
        // 1.获取 access_token
        AccessTokenResponse accessTokenResp = wechatFeign.getAccessToken(appid, secret, accesstokenType);
        String accessToken = accessTokenResp.getAccess_token();
        // 2.构建请求参数
        SendSubscribeMgRequest sendSubscribeMgRequest = SubscribeMsgUtils.buildSubscribeCustomerRequest(msgDTO);

        // 3.发送订阅消息
        SendSubscribeMsgResponse sendSubscribeMsgResponse = wechatFeign.sendSubscribeMsg(accessToken,sendSubscribeMgRequest);

        log.info("结果:{}",sendSubscribeMsgResponse);
        return DataResponse.succeed(sendSubscribeMsgResponse);
    }



}
